package com.hc.ssm.controller;

import com.hc.ssm.pojo.Member;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@RestController
public class MemberController {

    @RequestMapping("/getMember")
    public String getMember(){
        return "尊敬的王海军车主:王海军[9101]";
    }

    @RequestMapping("/getMemberById")
    public String getMemberById(Integer id){

        return "尊敬的拖拉机车主:王海军:"+id;
    }

    /**
     * 根据id查询会员信息，服务垄断的演示方法
     *  HystrixCommand服务垄断注解
     * @param id
     * @return
     * */
    @RequestMapping("/getMemberById2")
    @HystrixCommand(fallbackMethod = "processHystrix")
    public Member getMemberById2(Integer id){
        if(id==0){
           throw new RuntimeException("该"+id+"存在异常");//运行时异常
        }
        return new Member(id,"王海军");
    }

    public Member processHystrix(Integer id){
        return new Member(-1,"当前系统出现问题，请联系管理员。");
    }


    @RequestMapping("/getMemberList")
    public List<Member> getMemberList(){
        List<Member> list = new ArrayList<>();
        list.add(new Member(1,"王海军"));
        list.add(new Member(2,"王哈酒"));
        list.add(new Member(3,"王傻狗"));

        return list;
    }
    @RequestMapping("addMember")
    public Member addMember(@RequestBody Member member){
        System.out.println("*****************");
        System.out.println(member);
        System.out.println("*****************");
    return member;
    }



}
